package com.lemontree;

import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;

/**
 * @Author: YLBG-YCY-1325
 * @Description: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8
 * @Date: 2017/3/20
 */
public class Test {
    public static void main(String[] args) {
        Stream.iterate(1L, i -> i + 1).limit(5).reduce(0L, Long::sum);
        List<Employee> list = new ArrayList<>();
        list.add(new Employee(1250.0, "bose", "qt35"));
        list.add(new Employee(1850.0, "ATH", "msr7"));
        list.add(new Employee(1750.0, "IE8", "ie8"));
        list.add(new Employee(1680.0, "sony", "dac"));
        list.add(new Employee(1150.0, "AKG", "701"));
        list.add(new Employee(290.0, "AKG", "445"));

        Map<String, List<Employee>> map = list.stream().collect(groupingBy(Employee::getType));
        for (Map.Entry<String, List<Employee>> entry : map.entrySet()) {
            System.out.print(entry.getKey() + ":");
            System.out
                    .print(entry.getValue().stream().map(Employee::getName).collect(joining(",")));
            System.out.println();
        }
        String s = list.stream().map(Employee::getName).collect(Collectors.joining("|"));
        System.out.println(s);
        System.out.println(BinaryOperator.maxBy(Integer::compare));
    }

}

class DoubleCollector<T> implements Collector<T, List<T>, List<T>> {

    @Override
    public Supplier supplier() {
        return ArrayList::new;
    }

    @Override
    public BiConsumer<List<T>, T> accumulator() {
        return List::add;
    }

    @Override
    public BinaryOperator<List<T>> combiner() {
        return (left, right) -> {
            left.addAll(right);
            return left;
        };
    }

    @Override
    public Function finisher() {
        return Function.identity();
    }

    @Override
    public Set<Characteristics> characteristics() {
        return Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.UNORDERED,
                Collector.Characteristics.IDENTITY_FINISH));
    }
}
